pub mod cmd;

use base::base::{Base, VBase, VMap};
use base::{MapSv, Resp};
use moka::future::Cache;
use serde_json::{json, Value};
use warp::{
    hyper::body::Bytes,
    reject::Rejection,
    reply::{Json, WithStatus},
};

pub type SvCache = Cache<String, MapSv>;

pub async fn do_cmd(
    data: Bytes,
    cmd: String,
    cache: SvCache,
) -> Result<WithStatus<Json>, Rejection> {
    let val = match base::do_byte(data) {
        Ok(v) => v,
        Err(e) => {
            return Ok(base::ret_err(&e, None));
        }
    };

    match cmd::doit(&val, &cmd, cache).await {
        Ok(v) => Ok(base::ret_ok(&v)),
        Err(e) => Ok(base::ret_err(&e, None)),
    }
}
